package greedy;

/**
 * 摆动序列
 */
public class SwingSequence {

    public static void main(String[] args) {
        int[] nums = new int[] {1,17,5,10,13,15,10,5,16,8};
        int length = getMaxLengthOfSwingSubSequence(nums);
        System.out.println(length);
    }

    private static int getMaxLengthOfSwingSubSequence(int[] nums) {
        int result = 1;
        int prevDiff = 0;
        int currDiff = 0;
        for (int i = 1; i < nums.length; i++) {
            currDiff = nums[i] - nums[i-1];
            if ((prevDiff>=0 && currDiff<0) || (prevDiff <=0 && currDiff >0)) {
                result++;
                prevDiff = currDiff;
            }
        }
        return result;
    }
}
